package com.leador.http.callback;

import com.leador.http.ResultParse;
import com.leador.manger.LBSTraceClient;
import com.leador.module.response.Result;
import com.leador.module.response.service.ServiceResult;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by liuha on 2016/9/18.
 */
public class ServiceCallback {
    private static  ServiceResult serviceSucessCallback(String msg){
        ServiceResult result = new ServiceResult();
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(msg);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if(jsonObject == null){
            return null;
        }

        String status = null;
        try {
            status = jsonObject.getString("status");
            result.setStatus(status);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String message = null;
        try {
            message = jsonObject.getString("message");
            result.setMessage(message);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String serviceId = null;
        try {
            serviceId = jsonObject.getString("service_id");
            result.setServiceId(serviceId);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void serviceCallBack(int status,String message){
        if(LBSTraceClient.onServiceListener == null){
            return;
        }
        try{
            switch (status){
                case 0:
                    Result result = ResultParse.resultParse(message);
                    LBSTraceClient.onServiceListener.onRequestFailedCallback(result);
                    break;
                case 1:
                    ServiceResult serviceResult = serviceSucessCallback(message);
                    LBSTraceClient.onServiceListener.onRequestSucessCallback(serviceResult);
                    break;
            }
        }catch (Exception e){

        }
    }


}
